home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dialog / checkbox.c < prev    next >
C/C++ Source or Header  |  1996-07-31  |  3KB  |  152 lines

  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include "diadef.h"
  4. #include <ctype.h>
  5. #include "dialog.h"
  6.  
  7. PUBLIC FIELD_CHECK_RADIO::FIELD_CHECK_RADIO(
  8.     const char *_prompt,
  9.     char &_var,
  10.     const char *_title)
  11.     : FIELD (_prompt)
  12. {
  13.     title = strdup(_title);
  14.     var = &_var;
  15.     val = _var;
  16.     backup = val;
  17.     box.width = 4 + strlen(_title);
  18. }
  19.  
  20. PUBLIC FIELD_CHECK_RADIO::~FIELD_CHECK_RADIO()
  21. {
  22.     free (title);
  23. }
  24. PUBLIC void FIELD_CHECK_RADIO::restore()
  25. {
  26.     *var = backup;
  27. }
  28. PUBLIC void FIELD_CHECK_RADIO::save()
  29. {
  30.     *var = val;
  31. }
  32.  
  33. /*
  34.     Position the cursor in the field
  35. */
  36. PUBLIC void FIELD_CHECK_RADIO::setcursor(WINDOW *dialog)
  37. {
  38.     wmove (dialog,box.y,box.x+1);
  39. }
  40.  
  41. /*
  42.     Draw only the input part of a field
  43. */
  44. PUBLIC void FIELD_CHECK_RADIO::drawtxt_check (
  45.     WINDOW *dialog,
  46.     char openchar,        // Code to draw the left side of the button
  47.     char closechar,        // Right side
  48.     char selchar)        // Interior of the button
  49. {
  50.     wattrset(dialog, inputbox_attr);
  51.     wmove(dialog, box.y,box.x);
  52.     char buf[80];
  53.     int len = sprintf (buf,"%c%c%c %s",openchar,selchar,closechar,title);
  54.     waddstr (dialog,buf);
  55.     for (int i=len; i<box.width; i++) waddch (dialog,' ');
  56. }
  57.  
  58. /*
  59.     Build a key that uniquely identify this field in the dialog
  60. */
  61. PUBLIC void FIELD_CHECK_RADIO::format_htmlkey(char *key, int nof)
  62. {
  63.     char buf[200];
  64.     strcpy (buf,prompt);
  65.     strcat (buf,title);
  66.     html_formatkey (key,"%s-%d",buf,nof);
  67. }
  68.  
  69.  
  70. /* #Specification: dialog / checkbox
  71.     A checkbox is used to toggle an option. It edit a variable
  72.     from 0 to 1.
  73.  
  74.     The checkbox is presented like this
  75.  
  76.     #
  77.     prompt [ ] some explanation
  78.     prompt [X] some explanation
  79.     #
  80. */
  81. #
  82. PUBLIC FIELD_CHECK::FIELD_CHECK(
  83.     const char *_prompt,
  84.     char &_var,
  85.     const char *_title)
  86.     : FIELD_CHECK_RADIO (_prompt,_var,_title)
  87. {
  88. }
  89.  
  90.  
  91. /*
  92.     Draw only the input part of a field
  93. */
  94. PUBLIC void FIELD_CHECK::drawtxt (
  95.     WINDOW *dialog)
  96. {
  97.     drawtxt_check (dialog,'[',']',val ? 'X' : ' ');
  98. }
  99.  
  100. static const char YES[]="yes";
  101. static const char NO[]="no";
  102.  
  103. PUBLIC void FIELD_CHECK::html_draw(int nof)
  104. {
  105.     char key[100];
  106.     format_htmlkey (key,nof);
  107.     html_printf ("<tr><td>%s<td>",prompt);
  108.     html_defvar ("checkbox",key,YES,val ? "checked" : "");
  109.     html_printf ("%s\n",title);
  110.     html_defvarcur (key,val ? YES : NO);
  111. }
  112.  
  113. PUBLIC int FIELD_CHECK::html_validate(int nof)
  114. {
  115.     int ret = -1;
  116.     char key[100];
  117.     format_htmlkey (key,nof);
  118.     char oldval = stricmp(html_getoldval(key),YES)==0;
  119.     char newval = stricmp(html_getval(key),YES)==0;
  120.     if (val == oldval){
  121.         ret = 0;
  122.         val = newval;
  123.     }
  124.     return ret;
  125. }
  126.  
  127. PUBLIC void FIELD_CHECK::dokey (
  128.     WINDOW *dialog,
  129.     int key,
  130.     FIELD_MSG &)
  131. {
  132.     switch (key){
  133.     case ' ':
  134.         val = !val;
  135.         drawtxt (dialog);
  136.         break;
  137.     }
  138. }
  139.  
  140. /*
  141.     Add a check box field to the dialog.
  142. */
  143. PUBLIC FIELD_CHECK *DIALOG::newf_chk(
  144.     const char *prompt,
  145.     char &var,
  146.     const char *title)
  147. {
  148.     FIELD_CHECK *s = new FIELD_CHECK(prompt,var,title);
  149.     add (s);
  150.     return s;
  151. }
  152.